<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;


class CommentController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //首先用多表联查弄出显示的名字先    在查评论的时候做分页！！！！2016年8月24日10:18:06  通过点击商品查看评论
        $list = \DB::table('comment')->paginate(5);
        
        foreach($list as $v){

            //找到评论表里uid对应的用户的名字并取代原值
            $v->uid = \DB::table('user')->where('id',$v->uid)->value('userName');

            //找到评论表里bid对应的商品的名称并取代原值
            $v->bid = "《".\DB::table('goodsdetail')->where('id',$v->bid)->value('goodName')."》";
        }
        $list->name = null;
        return view('admin.comment.index',['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   
        //获取查询的关键字
        $name = $_GET['name'];

        //判断此关键字是否对应商品，有则获取其id组成的数组（对象），可能有多个！
        if($id = \DB::table('goodsdetail')->where('goodName','like',"%{$name}%")->get(['id'])){
            //直接跳到下一步
        }else{ 
            return back()->with('err',"未搜索到相关信息");
        }

        //找出所有的有id
        foreach($id as $k){
            $xixi[] = $k->id;
        }
        
        //跟后面做出来的list是一样的，不过就是多了个分页的属性
        $fenye = \DB::table('comment')->whereIn('bid',$xixi)->paginate(5);
        $fenye->name = $name;
        
        foreach($fenye as $test){
            //找到评论表里uid对应的用户的名字并取代原值
            $test->uid = \DB::table('user')->where('id',$test->uid)->value('userName');

            //找到评论表里bid对应的商品的名称并取代原值
            $test->bid = "《".\DB::table('goodsdetail')->where('id',$test->bid)->value('goodName')."》";
        }
        
        //返回里面的值
        return view('admin.comment.index',['list'=>$fenye]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return "loading";
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {     
        
        switch ($id) {
            case 'levelUp':
                $data = \DB::table('comment')->orderBy('level','asc')->paginate(5);
                $data->name = null;
                return view('admin.comment.index',['list'=>$data]);
            break;
            case 'levelDown':
                $data = \DB::table('comment')->orderBy('level','desc')->paginate(5);
                $data->name = null;
                return view('admin.comment.index',['list'=>$data]);
            break;
            case 'timeUp':
                $data = \DB::table('comment')->orderBy('time','asc')->paginate(5);
                $data->name = null;
                return view('admin.comment.index',['list'=>$data]);
            break;
            case 'timeDown':
                $data = \DB::table('comment')->orderBy('time','desc')->paginate(5);
                $data->name = null;
                return view('admin.comment.index',['list'=>$data]);
            break;
            case 'checkYes':
                $data = \DB::table('comment')->orderBy('check','desc')->paginate(5);
                $data->name = null;
                return view('admin.comment.index',['list'=>$data]);
            break;
            case 'checkNo':
                $data = \DB::table('comment')->orderBy('check','asc')->paginate(5);
                $data->name = null;
                return view('admin.comment.index',['list'=>$data]);
            break;
            default:
               return view('admin.comment.info',['list'=>'未知错误']);
            break;
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       if($_GET['status']=='forbidme'){
            // \DB::update('update comment set check=0 where id=?',[$id]);//这个会报错~~~！！
            \DB::table('comment')->where('id',$id)->update(['check'=>0]);
            return back()->with('CommentMSG','已响应修改');
       }else{
            // \DB::update('update comment set check=1 where id=?',[$id]);//不知道为什么！~
            \DB::table('comment')->where('id',$id)->update(['check'=>1]);
            return back()->with('CommentMSG',"已响应修改");
       }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($_POST);
        // $data = $_POST;
        // unset($data['_method']);
        // unset($data['_token']);
        // $m = \DB::table('comment')->where('id',$id)->update($data);
        // if($m>0){
        //     return view('admin.comment.info',['info'=>'嗯，你修改成功了呢']);
        // }else{
        //     return view('admin.comment.info',['info'=>'失败了失败了失败了~~']);
        // }
        return "loading";
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        // dd($id);
        // $m = \DB::table('comment')->where('id',$id)->delete();
        // if($m){
        //     return view('admin.comment.info',['info'=>'删掉了！！']);
        // }else{
        //     return view('admin.comment.info',['info'=>'没有删掉！！']);
        // }
        return "loading";
    }
}
